<template>
  <div>
    <!-- 轮播图 -->
    <swiper ref="mySwiper" :options="swiperOptions" width="100%">
      <swiper-slide v-for="(item, index) in img" :key="index"
        ><img :src="item.image" :alt="item.title" width="100%"
      /></swiper-slide>
      <!-- 下面的小点 -->
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>

    <!-- 下面的菜单按钮 -->
    <ul class="menu">
      <li
        v-for="(item, index) in img"
        :key="index"
        :class="{ active: index == num }"
        @click="changeSwiper(index)"
      >
        {{ item.title }}
      </li>
    </ul>
  </div>
</template>

<script>
import api from "@/api/index.js";
export default {
  name: "VueSwiper",
  data() {
    let that = this;
    return {
      swiperOptions: {
        on: {
          slideChangeTransitionStart: function () {
            // console.log(that.num);
            // swiperAnimate(this); //每个slide切换结束时也运行当前slide动画
            //this.slides.eq(this.activeIndex).find('.ani').removeClass('ani'); 动画只展现一次，去除ani类名
            if (this.activeIndex == that.arr.length + 1) {
              that.num = 0;
            } else {
              that.num = this.activeIndex - 1;
            }
          },
        },
        pagination: {
          el: ".swiper-pagination",
        },
        autoplay: {
          delay: 8000,
          stopOnLastSlide: false,
          disableOnInteraction: false,
        },
        loop: true,
      },
      arr: [1, 2, 3, 4, 5],
      num: 0,
      img: [],
    };
  },
  computed: {
    swiper() {
      //这玩意市是获取swpier 实例对象的
      return this.$refs.mySwiper.$swiper;
    },
  },
  mounted() {
    //获取数据
    api.getCovPic().then((res) => {
      if (res.data.status !== 200)
        return console.log("疫情统计数据图片获取出错");
      this.img = res.data.result;
      //console.log(this.img);
    });
  },
  methods: {
    changeSwiper(index) {
      //先是改变高亮
      (this.num = index),
        //改变swiper
        this.swiper.slideTo(index + 1, 1000, false);
    },
  },
};
</script>

<style lang="less" scoped>
.menu {
  display: flex;
  flex-wrap:wrap;
  margin:0.2rem 0 2rem 0.2rem;
  li {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0.1rem 0.25rem;
    box-sizing: border-box;
    padding: 0.2rem;
    color: #666;
    text-align: center;
    background: #f7f7f7;
    border-radius: 0.02rem;
    
  }
  .active {
    color: #4169e2;
    background: #f1f5ff;
  }
}
</style>